<?php
namespace app\common\controller;

use mortalauth\Mortalauth;
use think\facade\Config;


class Adminbase extends Base
{
    protected $auth_check;
    protected $this_user;
    protected $page_info=[
        'title' =>  'Mortal BBS',
        'description' =>  'Mortal BBS',
        'keywords' =>  'Mortal BBS'
    ];
    /**
     * 公共列表页/添加/编辑/删除/上传等url定义
     * 支持继承此控制器重构             关闭添加按钮                   关闭批量删除按钮               关闭右侧编辑按钮                关闭右侧删除按钮
     * $operate = $this->list_page;$operate += ['bar_add'=>1];$operate += ['bar_delete'=>1];$operate += ['r_bar_edit'=>1];$operate += ['r_bar_delete'=>1];
     * ,'operate'=>$operate
     * @var
     */
    protected $list_page;
    protected function initialize(){
        parent::initialize();

        $this->auth_check = new Mortalauth;
        //登录检查
        if($is_login = $this->auth_check->check_login()){
            if(is_array($is_login) && ($is_login['code'] == -1)){
                $this->error($is_login['msg'],'/admin/login/index');
            }
        }
        //登录检查end
        $this->this_user = session(Config::get('mortalauth.login_state'));
        //权限检查
        $route_url = strtolower(request()->module() . '/' . request()->controller() . '/' . request()->action());
        $res = $this->auth_check->check_auth($this->this_user['id'],$route_url);
        if($res && is_array($res)){
            if($res['code'] == -1){
                if(request()->isPost() || request()->isAjax()){
                    header('Content-Type: application/json');
                    echo json_encode(['code'=>1,'msg'=>$res['msg']],JSON_UNESCAPED_UNICODE);
                    exit;
                }else{
                    http_response_code(404);
                    echo $res['msg'];
                    exit;
                }
            }
        }
        //权限检查end
        //路由节点以及非路由节点DEBUG日志
        $route_info = db(Config::get('mortalauth.auth_menu'))->where(['conditions'=>strtolower($route_url)])->find();
        $info = request()->param();
        if(is_array($info)){
            $info = $info ? json_encode($info, JSON_UNESCAPED_UNICODE) : null;
        }
        if(!$info){
            $info = $_FILES ? json_encode($_FILES, JSON_UNESCAPED_UNICODE): null;
        }
        $log_info['ip'] = get_ip_address(request()->ip());
        $log_info['routes'] = strtolower($route_url);
        $log_info['access'] = $_SERVER['REQUEST_METHOD'];
        $log_info['create_at'] = date('Y-m-d h:i:s',time());
        if($route_info){
            $this->add_log($route_info,$log_info,$info);
        }else{
            if(get_s_c('no_r_log') == '1'){
                $log_info['describes'] = '非权限路由日志：访问页面['.$info.']';
                $log_info['uid'] = "ID:".$this->this_user['id']."用户名：".$this->this_user['account'];
                db('logs')->insert($log_info);
            }
        }
        $this->page_info['title'] = $route_info['name'];
        $this->assign('page_info',$this->page_info);
        $operate_s = request()->module() . '/' . request()->controller();
        $pages = [
            'save'=>$operate_s . '/' .'save',
            'add'=>$operate_s . '/' .'add',
            'edit'=>$operate_s . '/' .'edit',
            'update'=>$operate_s . '/' .'update',
            'delete'=>$operate_s . '/' .'delete',
            'upload'=>$operate_s . '/' .'upload',
        ];
        $this->list_page = $pages;
        $this->assign('operate',$pages);
        $this->assign('table_id',request()->action().rand(1,999));
        $this->assign('admin_user',$this->this_user);
    }
    public function add_log($route,$info,$param){
        $data['access'] = $info['access']?$info['access']:null;
        $data['ip'] = $info['ip']?$info['ip']:null;
        $data['routes'] = $info['routes']?$info['routes']:null;
        $data['uid'] = "ID:".$this->this_user['id']."用户名：".$this->this_user['account'];
        $data['create_at'] = date('Y-m-d h:i:s',time());
        if($route['log_type'] === 2){
            $data['describes'] = $param;
        }
        if($route['log_type'] == 1 || $route['log_type'] == 2){
            db('logs')->insert($data);
        }
    }
    public function get_user_menu(){
        $auth = db(Config::get('mortalauth.auth_table'))->where('id',$this->this_user[Config::get('mortalauth.auth_id')])->find();
        if($auth['conditions'] == Config::get('mortalauth.auth_super')){
            return $this->auth_check->get_user_menu();
        }else{
            return $this->auth_check->get_user_menu($auth['conditions']);
        }
    }
    public function update_info($table,$data){
        if(!$table){
            return ['code'=>1,'msg'=>'table empty'];
        }
        if(!$data){
            return ['code'=>1,'msg'=>'data empty'];
        }
        if(!isset($data['id'])){
            return ['code'=>1,'msg'=>'id empty'];
        }
        unset($data['file']);
        $data['update_at'] = date('Y-m-d h:i:s',time());
        $res = db($table)->where('id',$data['id'])->update($data);
        if($res){
            return ['code'=>200,'msg'=>'更新数据成功'];
        }else{
            return ['code'=>1,'msg'=>'更新数据失败'];
        }
    }
    public function add_info($table,$data){
        if(!$table){
            return ['code'=>1,'msg'=>'table empty'];
        }
        if(!$data){
            return ['code'=>1,'msg'=>'data empty'];
        }
        unset($data['file']);
        $data['create_at'] = date('Y-m-d h:i:s',time());
        $res = db($table)->insert($data);
        if($res){
            return ['code'=>200,'msg'=>'添加数据成功'];
        }else{
            return ['code'=>1,'msg'=>'添加数据失败'];
        }
    }
}